package com.data.modules.online.cgform.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.data.modules.online.cgform.entity.OnlCgformField;
import com.data.modules.online.cgform.enums.CgformValidPatternEnum;

public class OnlineImportValidator {

    public static final String f336a = "error";

    public static final String f337b = "tip";

    public static final String f338c = "filePath";

    private static final String f339e = ",";

    private static final String f340f = "第%s行校验信息:";

    private static final String f341g = "总上传行数:%s,已导入行数:%s,错误行数:%s";

    private Map<String, OnlCgformField> f342d;

    public OnlineImportValidator() {
    }

    public OnlineImportValidator(List<OnlCgformField> list) {
        this.f342d = new HashMap();
        for (OnlCgformField onlCgformField : list) {
            String fieldValidType = onlCgformField.getFieldValidType();
            if (fieldValidType != null && !"".equals(fieldValidType) && !CgformValidPatternEnum.ONLY.getType().equals(fieldValidType)) {
                this.f342d.put(onlCgformField.getDbFieldName(), onlCgformField);
            }
            if (onlCgformField.getDbIsNull().intValue() == 0 || "1".equals(onlCgformField.getFieldMustInput())) {
                this.f342d.put(onlCgformField.getDbFieldName(), onlCgformField);
            }
        }
    }

    public String checkFieldValidType(String str, int i) {
        String msg;
        StringBuffer stringBuffer = new StringBuffer();
        JSONObject parseObject = JSON.parseObject(str);
        for (String str2 : this.f342d.keySet()) {
            String string = parseObject.getString(str2);
            OnlCgformField onlCgformField = this.f342d.get(str2);
            String fieldValidType = onlCgformField.getFieldValidType();
            if (CgformValidPatternEnum.NOTNULL.getType().equals(fieldValidType) || onlCgformField.getDbIsNull().intValue() == 0 || "1".equals(onlCgformField.getFieldMustInput())) {
                if (string == null || "".equals(string)) {
                    stringBuffer.append(onlCgformField.getDbFieldTxt() + CgformValidPatternEnum.NOTNULL.getMsg() + ",");
                }
            } else if (string != null && !"".equals(string)) {
                if (CgformValidPatternEnum.INTEGER.getType().equals(fieldValidType)) {
                    fieldValidType = "^-?[1-9]\\d*$";
                    msg = "请输入整数";
                } else {
                    CgformValidPatternEnum patternInfoByType = CgformValidPatternEnum.getPatternInfoByType(fieldValidType);
                    if (patternInfoByType == null) {
                        msg = "校验【" + fieldValidType + "】未通过";
                    } else {
                        fieldValidType = patternInfoByType.getPattern();
                        msg = patternInfoByType.getMsg();
                    }
                }
                if (!Pattern.compile(fieldValidType).matcher(string).find()) {
                    stringBuffer.append(onlCgformField.getDbFieldTxt() + msg + ",");
                }
            }
        }
        if (stringBuffer.length() > 0) {
            return formatCheck(stringBuffer.toString(), i);
        }
        return null;
    }

    public static String formatCheck(String str, int i) {
        return String.format(f340f, Integer.valueOf(i)) + str + "\r\n";
    }

    public static String formatRemind(int i, int i2) {
        return String.format(f341g, Integer.valueOf(i), Integer.valueOf(i - i2), Integer.valueOf(i2));
    }
}
